package cn.larry.common.annotation;

import cn.larry.common.exception.Wechath5handlersException;
import cn.larry.common.utils.RequestUtils;
import cn.larry.common.utils.ServletUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;

import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.NotNull;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

/**
 * larry
 * 2022/12/26 22:20
 *
 * @Version 1.0
 */
@Slf4j
public abstract class Wechath5CheckerAdaptor implements Wechath5Checker {

    /**
     * （在'Controller'之前执行）
     *
     * @param object
     * @param methodName
     * @return
     */
    @Override
    public Object check(@NotNull Object object, String methodName) throws Wechath5handlersException {
        return object;
    }

    /**
     * 环绕前置切面（在'check'之前执行）
     *
     * @param object
     */
    @Override
    public Object aroundBefore(Object object) throws Wechath5handlersException {
        return object;
    }

    /**
     * 环绕后置切面（在'Controller'之后执行）
     *
     * @param object1
     * @param object2
     */
    @Override
    public Object aroundAfter(Object object1, Object object2) throws Wechath5handlersException {
        return object2;
    }

    /**
     * 获取真实IP
     */
    protected String getReqRealIp() {
        HttpServletRequest request = getHttpServletRequest();
        return RequestUtils.getIPAddress(request);
    }

    /**
     * 获取请求接口类型
     */
    protected String getReqUrl() {
        HttpServletRequest request = getHttpServletRequest();
        String url = request.getRequestURL().toString();
        return url;
    }

    /**
     * 获取请求接口类型
     */
    protected String getReqUri() {
        HttpServletRequest request = getHttpServletRequest();
        return request.getRequestURI().toString();
    }

    /**
     * 获取请求头信息
     *
     * @return
     */
    protected Map<String, String> getHeades() {
        HttpServletRequest request = getHttpServletRequest();
        Map<String, String> headesMap = new HashMap<String, String>();
        Enumeration<String> headerNames = request.getHeaderNames();
        while (headerNames.hasMoreElements()) {
            String element = headerNames.nextElement();
            headesMap.put(element, request.getHeader(element));
        }
        return headesMap;
    }

    /**
     * 获取HttpServletRequest
     */
    private HttpServletRequest getHttpServletRequest() {
        return ServletUtils.getRequest();
    }

    protected void showReq() {
        HttpServletRequest request = getHttpServletRequest();
        Map<String, String> headesMap = new HashMap<String, String>();
        Enumeration<String> headerNames = request.getHeaderNames();
        while (headerNames.hasMoreElements()) {
            String element = headerNames.nextElement();
            headesMap.put(element, request.getHeader(element));
        }
        log.info("请求头：\n{}", headesMap);

        log.info("请求报文：");
        Enumeration<String> parameterNames = request.getParameterNames();
        while (parameterNames.hasMoreElements()) {
            String name = parameterNames.nextElement();
            //根据请求数据
            String value = request.getParameter(name);
            log.info(name + "：" + value);
        }
    }
}
